package edu.pitt.dbmi.odie.gapp.gwt.client.util.picker.gdocs;

import java.util.ArrayList;

import com.allen_sauer.gwt.log.client.Log;
import com.smartgwt.client.core.Rectangle;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.widgets.Dialog;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.FormItemIcon;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.form.fields.events.IconClickEvent;
import com.smartgwt.client.widgets.form.fields.events.IconClickHandler;
import com.smartgwt.client.widgets.form.fields.events.KeyDownEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyDownHandler;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import edu.pitt.dbmi.odie.gapp.gwt.client.util.ODIE_ClientUtils;
import edu.pitt.dbmi.odie.gapp.gwt.client.util.panel.ODIE_ButtonItemPanel;

public class ODIE_GDocsPickerItem extends TextItem implements
		RecordClickHandler {
	
	private String directoryHandle ;

	private String title ;

	private static ODIE_GDocsPickerItem currentEditor;

	private static Dialog dialog;

	private static ListGrid gDocsPickerGrid = null;

	private static ButtonItem selectButtonItem;

	private static ButtonItem cancelButtonItem;

	private static void makeDialog() {
		dialog = new Dialog();
		dialog.setTitle("Google Docs Directory Picker") ;
		dialog.setAutoCenter(true);
		dialog.setIsModal(false);
		dialog.setShowHeader(true);
		dialog.setShowEdges(true);
		dialog.setEdgeSize(5);
		// dialog.setWidth(500);
		// dialog.setHeight(400);

		dialog.setShowToolbar(false);
		dialog.setWidth(325);
		dialog.setHeight(200);

		gDocsPickerGrid = buildGDocsPickerGrid();

		// Map bodyDefaults = new HashMap();
		// bodyDefaults.put("layoutLeftMargin", 5);
		// bodyDefaults.put("membersMargin", 10);
		// dialog.setBodyDefaults(bodyDefaults);

		dialog.addItem(gDocsPickerGrid);
		dialog.addItem(buildActionsPanel());

	}

	private static ListGrid buildGDocsPickerGrid() {

		final ListGrid gDocsGrid = ODIE_ClientUtils.buildListGrid();

		DataSource directoriesDS = buildDirectoryDataSource();

		gDocsGrid.setDataSource(directoriesDS);
		gDocsGrid.addRecordClickHandler(currentEditor);
		gDocsGrid.setAutoFetchData(true);
		gDocsGrid.setCanEdit(false);

		gDocsGrid.setAlternateRecordStyles(true);
		gDocsGrid.setShowAllRecords(true);
		gDocsGrid.setAutoFetchData(true);
		gDocsGrid.setCanEdit(false);
		gDocsGrid.setSelectionType(SelectionStyle.SINGLE);
		gDocsGrid.setCanHover(false);
		gDocsGrid.setCanSelectAll(false);
		gDocsGrid.setShowHeader(false);
		gDocsGrid.setShowHover(false);

		return gDocsGrid;

	}

	public static DynamicForm buildActionsPanel() {
		selectButtonItem = ODIE_ClientUtils.buildButtonItem("Select",
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						Record selectedRecord = gDocsPickerGrid
								.getSelectedRecord();
						if (selectedRecord != null) {
							String id = selectedRecord.getAttribute("id") ;
							String title = selectedRecord.getAttribute("title");
							if (id != null && title != null) {
								currentEditor.setDirectoryHandle(id) ;
								currentEditor.setTitle(title) ;
								currentEditor.setValue(currentEditor.getTitle());
							}
						}
						gDocsPickerGrid.deselectAllRecords();
						dialog.hide();
					}
				});
		selectButtonItem.setDisabled(true);
		cancelButtonItem = ODIE_ClientUtils.buildButtonItem("Cancel",
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						gDocsPickerGrid.deselectAllRecords();
						dialog.hide();
					}
				});
		ArrayList<ButtonItem> buttons = new ArrayList<ButtonItem>();
		buttons.add(selectButtonItem);
		buttons.add(cancelButtonItem);
		return new ODIE_ButtonItemPanel("Actions", buttons);
	}

	private static DataSource buildDirectoryDataSource() {
		DataSource dataSource = new DataSource();
		dataSource.setShowPrompt(false) ; // Turns of loading dialog?
		dataSource.setDataFormat(DSDataFormat.XML);
		dataSource.setRecordXPath("//atom:entry");
		dataSource.setDataURL("odie_client/document");
		DataSourceTextField idField = new DataSourceTextField("id", "id");
		idField.setHidden(true);
		DataSourceTextField displayLabelField = new DataSourceTextField(
				"title", "Title");
		displayLabelField.setTitle(null);
		dataSource.setFields(idField, displayLabelField);
		return dataSource;
	}

	public void onRecordClick(RecordClickEvent event) {
		this.selectButtonItem.setDisabled(false);
	};

	// show the picker dialog at the specified position
	private static void showDialog(int left, int top) {
		dialog.show();
		dialog.moveTo(left, top);
	}

	public ODIE_GDocsPickerItem() {
	
		addKeyPressHandler(new KeyPressHandler() {
			public void onKeyPress(KeyPressEvent event) {
				Log.debug("Canceling chracter " + event.getCharacterValue()) ;
				event.cancel();
			}}) ;
		
		// use default trigger icon here. User can customize.
		// [SKIN]/DynamicForm/default_formItem_icon.gif
		FormItemIcon formItemIcon = new FormItemIcon();
		setIcons(formItemIcon);

		addIconClickHandler(new IconClickHandler() {
			public void onIconClick(IconClickEvent event) {

				// get global coordinates of the clicked picker icon
				Rectangle iconRect = getIconPageRect(event.getIcon());

				// remember what editor is active, so we can set its value from
				// the picker dialog
				ODIE_GDocsPickerItem.currentEditor = ODIE_GDocsPickerItem.this;

				// lazily create the YesNoMaybe picker dialog the first time a
				// yesNoMaybe editor is clicked
				if (ODIE_GDocsPickerItem.dialog == null) {
					ODIE_GDocsPickerItem.makeDialog();
				}

				// show the picker dialog
				ODIE_GDocsPickerItem.showDialog(iconRect.getLeft(), iconRect
						.getTop());
			}
		});
	}
	
	
	public String getDirectoryHandle() {
		return directoryHandle;
	}

	public void setDirectoryHandle(String directoryHandle) {
		this.directoryHandle = directoryHandle;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

}
